<template>
	<view class="login">

		<view class="login_box">
			<view class="username">
				<text>手机号</text>
				<input type="text" placeholder="请输入手机号" v-model="phone" />
			</view>
			<view class="username">
				<text>验证码</text>
				<view class="codebox">
					<input type="text" placeholder="请输入验证码" v-model="code" />
					<button class="code" @click="SendCode" v-show="is_send">获取验证码</button>
					<button class="code" v-show="!is_send">{{time}}s</button>
				</view>
			</view>

			<button class="btn primary" @click="login" :loading="loading" :disabled="loading">
				登录
			</button>
			<!-- <view class="wxlogin">
				—— <image src="/static/index/wx.png" mode="widthFix"></image> ——
			</view> -->

		</view>
	</view>
</template>

<script>
	import {
		sendCode,
		userLogin
	} from "@/api/api.js"
	export default {
		data() {
			return {
				phone: '',
				password: '',
				loading: false,
				time: 60,
				timer: '',
				code: '',
				is_send: true,
				invite: ''
			}
		},
		onLoad(e) {
			if (e.invite) {
				this.invite = e.invite
			}
		},
		methods: {
			getPhone() {
				if (this.phone == '') {
					uni.showToast({
						title: '请输入手机号',
						icon: "none"
					})
					return false
				}
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phone))) {
					uni.showToast({
						title: '请填写正确手机号码',
						icon: "none"
					});
					return false;
				}
				return true
			},
			SendCode() {
				if (!this.getPhone()) {
					return false
				}
				this.is_send = false
				this.time = 60
				sendCode({
					phone: this.phone
				}).then(res => {
					uni.$showMsg(res.msg)
					if (res.code == 0) {
						this.timer = setInterval(() => {
							this.time--;
							if (this.time < 0) {
								clearInterval(this.timer)
								this.is_send = true
							}
						}, 1000)
					}
				})
			},
			login() {
				if (this.code == '') {
					return uni.$showMsg('请输入验证码')
				}
				this.loading = true
				userLogin({
					phone: this.phone,
					code: this.code,
					invite: this.invite
				}).then(res => {
					this.loading = false
					if (res.code == 0) {
						uni.setStorageSync('tokens', res.data.token)
						uni.navigateBack()
					} else {
						uni.$showMsg(res.msg)
					}
				})
			},
			toRegister() {
				uni.redirectTo({
					url: '/pages/register/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	.login {
		padding-top: 20px;
		font-size: 16px;
		height: calc(100vh - 64px);

		.codebox {
			position: relative;

			input {
				padding-right: 120px;
			}

			.code {
				position: absolute;
				right: 10px;
				top: 50%;
				transform: translate(0, -50%);
				font-size: 12px;
				width: 90px;
				background: $uni-bg-color;
				color: #fff;
			}
		}

		.title {
			display: flex;
			justify-content: space-around;
			align-items: center;
			color: #fff;
			width: 50%;
			margin: 0 auto;

			view {
				display: flex;
				flex-direction: column;
				align-items: center;
			}

			text {
				display: block;
				width: 30px;
				height: 3px;
				background: #fff;
				margin-top: 3px;
			}
		}

		.active {
			font-size: 24px;
		}

		.login_box {
			background: $uni-bg-color;
			width: 80%;
			padding: 20px 5%;
			border-radius: 8px;
			margin: 10vh auto;

			text {
				margin: 15px 0;
				display: block;
				font-weight: bold;
				font-size: 15px;
				color: #fff;
			}

			input {
				border: 1px solid $uni-bg-color;
				outline: 0;
				border-radius: 5px;
				height: 40px;
				padding-left: 15px;
				margin-top: 15px;
				font-size: 14px;
				background: #fff;
			}
		}

		.wxlogin {
			text-align: center;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #ccc;

			image {
				width: 30px;
				margin: 0 10px;
			}
		}

		.tips {
			display: flex;
			justify-content: flex-end;
			font-size: 14px;
			align-items: center;

			text {
				font-weight: normal;
				color: #777;
			}

			.forget {
				color: $uni-text-color;
			}
		}


	}
</style>